import { BindNode } from "../../Bridge/BindNode";
import { GoWarp } from "../../Bridge/GoWarp";
import { App } from "../Core/App";
import NodeHelper from "../../Bridge/NodeHelper";
import Util from "../Util/Util";
import { Stack } from "./Stack";


export class NodePool
{
    private _pool:Stack<GoWarp> = new Stack<GoWarp>();
    seedGoWarp:GoWarp;

    constructor(seedWarp:GoWarp)
    {
        // console.assert(goInst != null , false);    
        this.seedGoWarp = seedWarp; //种子不入库
        this.seedGoWarp.name = this.seedGoWarp.name + "_seed";
        this.seedGoWarp.SetParent(App.Instance.poolMgr.poolRoot);
        seedWarp.SetActive(false);
        var firstOne =  NodeHelper.Instantiate(seedWarp.bindNode)
        firstOne.SetParent(App.Instance.poolMgr.poolRoot); 
        firstOne.SetActive(false);
        var goWarpNew = new GoWarp(firstOne);
        goWarpNew.poolName = seedWarp.poolName;
        this.Put(goWarpNew);
    }

    public Get():GoWarp
    {
        if (this._pool.Count == 0)
        {
            // var one = NodeHelper.Instantiate(this.seedGoWarp.bindNode);
            // var newOne = this.seedGoWarp.Instantiate();
            var one =  NodeHelper.Instantiate(this.seedGoWarp.bindNode);
            var goWarpNew = new GoWarp(one);
            goWarpNew.poolName = this.seedGoWarp.poolName;
            return goWarpNew;
        }
        return this._pool.Pop();
    }

    public Put(warp:GoWarp)
    {
        warp.SetParent(App.Instance.poolMgr.poolRoot); 
        warp.SetActive(false);
        this._pool.Push(warp);
    }
}